
package artistic;

import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */

public class TestTool implements PaintTool
{
    private Brush brush;

    public TestTool(ToolBox toolBox)
    {
        brush = new SimpleBrush(toolBox);
    }

    public Icon getIcon() {
        URL url = getClass().getResource("images/testicon.png");
        return new ImageIcon(url);
    }

    private boolean isHeld = false;
    private boolean wasHeld = false;
    private int lastX, lastY;

    public void mouseCallback(PaintWindow target, MouseEvent e) {
        wasHeld = isHeld;
        if(e.getID() == MouseEvent.MOUSE_RELEASED)
            isHeld = false;
        if(e.getID() == MouseEvent.MOUSE_PRESSED)
            isHeld = true;

        if(e.getID() != MouseEvent.MOUSE_PRESSED &&
           e.getID() != MouseEvent.MOUSE_DRAGGED)
            return;

        if(!isHeld)
            return;

        int x = e.getX();
        int y = e.getY();

        PaintImage image = target.getImage();
        WorkCanvas canvas = image.createWorkLayer();

        if(false)//isHeld && wasHeld)
        {
            brush.drawLine(canvas, lastX, lastY, x, y);
        }
        else
        {
            brush.draw(canvas, x, y);
        }

        lastX = x;
        lastY = y;

        canvas.done();

        target.repaint();
    }

    public void mouseWheelCallback(PaintWindow target, MouseWheelEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
